package com.sakura.probe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.probe.common.util.TimeCalculateUtil;
import com.sakura.probe.dto.Statistics;
import com.sakura.probe.mapper.*;
import com.sakura.probe.nosql.elasticsearch.EsSearchService;
import com.sakura.probe.nosql.redis.RedisKey;
import com.sakura.probe.nosql.redis.RedisService;
import com.sakura.probe.pojo.*;
import com.sakura.probe.service.PersonInfoService;
import com.sakura.probe.service.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class PersonInfoServiceImpl extends ServiceImpl<PersonInfoMapper, PersonInfo> implements PersonInfoService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    private PersonInfoMapper personMapper;
    @Autowired
    private FootPrintMapper footPrintMapper;
    @Autowired
    private FeedBackMapper feedBackMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private TopicImgMapper imgMapper;
    @Autowired
    private EsSearchService esSearchService;

    @Override
    public Map<String, Object> getPersonTopicInfo(Long userId) {
        Map<String,Object> modelMap = new HashMap<>();
        try {
            //该用户的发帖数
            Integer topicNum = topicMapper.selectCount(new QueryWrapper<Topic>()
                    .eq("owner_id", userId).eq("enable_status", 1));
            modelMap.put("topicNum",topicNum);
            //该用户的点赞数
            String thumbKey = RedisKey.getPersonThumb() + userId;
            Set<String> thumbSet = redisService.getSet(thumbKey);
            if (null==thumbSet){
                modelMap.put("thumbNum",0);
            }else {
                modelMap.put("thumbNum",thumbSet.size());
                redisService.setSet(thumbKey,thumbSet);
            }
            //该用户的收藏数
            String collectKey = RedisKey.getPersonCollect() + userId;
            Set<String> collectSet = redisService.getSet(collectKey);
            if (null==collectSet){
                modelMap.put("collectNum",0);
            }else {
                modelMap.put("collectNum",collectSet.size());
                redisService.setSet(collectKey,collectSet);
            }
            modelMap.put("success",true);
        } catch (Exception e) {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @Override
    public void getPersonStaticData(Map<String, Object> modelMap) {
        List<Statistics> statisticsList = personMapper.statPerson();
        statPersonCount(statisticsList,modelMap);
    }

    @Override
    public boolean deletePerson(Long userId) {
        try {
            //先删除pb_footprint表中用户相关联的数据
            footPrintMapper.delete(new QueryWrapper<FootPrint>().eq("user_id",userId));
            //接着删除pb_feedback表中用户相关联的数据
            feedBackMapper.delete(new QueryWrapper<FeedBack>().eq("user_id",userId));
            //然后删除topic表相关联的数据
            commentMapper.delete(new QueryWrapper<Comment>().eq("topic_reviewer_id",userId));
            //获取要删除用户发布的话题
            List<Topic> topicList = topicMapper.selectList(new QueryWrapper<Topic>().eq("owner_id", userId));
            for (Topic topic:topicList){
                topicService.deleteTopic(topic.getTopicId());
            }
            //最后删除用户信息

            personMapper.deleteById(userId);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean batchDeletePerson(List<Long> list) {
        try {
            if (null!=null&&!list.isEmpty()){
                for (Long userId:list){
                    deletePerson(userId);
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 统计前7天的用户注册量
     */
    private void statPersonCount(List<Statistics> list, Map<String, Object> modelMap) {
        Map<String,Long> hashMap = new HashMap<>();
        for (Statistics stat:list){
            hashMap.put(stat.getDay(),stat.getData());
        }
        LinkedList<String> day = new LinkedList<>();
        LinkedList<Long> count = new LinkedList<>();
        for (int i=0;i<7;i++){
            String nowTime = TimeCalculateUtil.beforeTime(i);
            if (hashMap.containsKey(nowTime)){
                count.addFirst(hashMap.get(nowTime));
            }else {
                count.addFirst(0L);
            }
            day.addFirst(nowTime);
        }
        modelMap.put("day",day);
        modelMap.put("count",count);

    }


}
